home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / 3dkit1.zip / 2DTOEXF.C next >
Text File  |  1992-05-25  |  2KB  |  74 lines

  1. /* 2DtoEXF  -  convert 2-D list to Evolution Computing (EasyCAD, FastCAD)
  2.                 Echange File Format (.EXF) */
  3.  
  4. /* Oscar Garcia <garciao@mof.govt.nz>, May 1992 */
  5.  
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8.  
  9. #define USAGE "usage: 2DtoEXF [infile [outfile]]\n\
  10. \tIf file names are omitted, the standard i/o streams are used.\n"
  11.  
  12. #define ERROR(msg) {fputs(msg,stderr),exit(1);}
  13. #define PERROR(msg) {perror(msg),exit(1);}
  14.  
  15.  
  16. #define RECLEN 81
  17.  
  18. void main(int argc, char* argv[])
  19. {
  20.     int c;
  21.     double x, y, x0 = 0, y0 = 0;
  22.     char comment[RECLEN + 1];
  23.     FILE *input = stdin, *output = stdout;
  24.  
  25.     /* open files */
  26.     if (argc > 3)
  27.         ERROR(USAGE);   /* wrong arguments */
  28.     if (argc > 1)
  29.     {    input = fopen(argv[1], "rt");
  30.         if (input == NULL)
  31.         {    fputs("Can't open input file\n", stderr);
  32.             ERROR(USAGE);
  33.         }
  34.     }
  35.     if (argc > 2)
  36.     {    output = fopen(argv[2], "wt");
  37.         if (output == NULL)
  38.             ERROR("Can't open output file");
  39.     }
  40.  
  41.     /* fake file name */
  42.     fputs("3DV.DWG\n\n", output);
  43.  
  44.     /* transfer comments */
  45.     while ((c = getc(input)) == '#')
  46.     {    fgets(comment, RECLEN, input);
  47.         putc(';', output);
  48.         fputs(comment, output);
  49.     }
  50.     if (ferror(input))
  51.         PERROR("Bad file");
  52.     ungetc(c, output);
  53.  
  54.     /* do it */
  55.     while (3 == fscanf(input, "%lf %lf %d", &x, &y, &c))
  56.     {    if (c != 0)
  57.         {    /* draw */
  58.             fprintf(output, "LINE\n1,%d,%g,%g,%g,%g,1\n\n",
  59.                 c, x0, y0, x, y);
  60.         }
  61.         x0 = x;
  62.         y0 = y;
  63.     }
  64.  
  65.     if (ferror(input))
  66.         PERROR("Error on input");
  67.     fclose(input);
  68.  
  69.     fputs("EOF\n", output);
  70.     if (ferror(output))
  71.         PERROR("Error on output");
  72.     fclose(output);
  73. }
  74.